package org.opensourcedea.ui.osdeagui;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.opensourcedea.dea.*;

public class SaveAsDEAProblemAction extends Action implements IWorkbenchAction {
	
	
	private static final String ID = "org.opensourcedea.ui.osdeagui.OpenDEAProblemAction";
	private static final String[] filterNames = {
		"DEA Problem (*.deap)"
	};
	private static final String[] filterExts = {
		"*.deap"
	};
	private Display display;
	
	
	public SaveAsDEAProblemAction() {
		setId(ID);
		
		Display.getDefault().syncExec( new Runnable() {  public void run() { } });
		
	}
	
	
	public void run() {

		
	    final Shell shell = new Shell(display);
	    

	    FileDialog dlg = new FileDialog(shell, SWT.SAVE);
	    dlg.setFilterNames(filterNames);
	    dlg.setFilterExtensions(filterExts);
	    
	    
	    try {
	    	String fileName = dlg.open();
		    if (fileName != null) {
				String ext="";
				int pos= fileName.lastIndexOf(".");
				ext=fileName.substring(pos+1,fileName.length());  
				if(ext != ".deap"){
					fileName.concat(".deap");
				}
		      try {
	                FileOutputStream fOut = new FileOutputStream(fileName);
	                ObjectOutputStream out = new ObjectOutputStream(fOut);
	                out.writeObject(createDEAProblem());
	                out.close();
	                fOut.close();
	            }
	            catch(Exception c) {
	                c.printStackTrace();
	            }
		    }
	    }
	    catch (Exception e) {
			System.out.println(e.toString());
	    }

	    if(display !=null){
	    	display.dispose();
	    }

	  
		
	}
	

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}
	
	
	private DEAProblem createDEAProblem() {
		
	    String[] testDMUNames = new String[7];
	    String[] testVariableNames = new String [3];
	    VariableOrientation[] testVariableOrientations = new VariableOrientation[3];
	    double[] [] testDataMatrix = new double[7] [3];
	    DEAProblem rdeaP = new DEAProblem(7,3);
	    
	    
	    
	  //Set up the DMU Names
        testDMUNames[0] = "DMU A";
        testDMUNames[1] = "DMU B";
        testDMUNames[2] = "DMU C";
        testDMUNames[3] = "DMU D";
        testDMUNames[4] = "DMU E";
        testDMUNames[5] = "DMU F";
        testDMUNames[6] = "DMU G";

        //Set up the Variable Names
        testVariableNames[0] = "Wood";
        testVariableNames[1] = "Twigs";
        testVariableNames[2] = "Fire";

        //Set up the Data Matrix
        testDataMatrix [0] [0] = 4;
        testDataMatrix [0] [1] = 3;
        testDataMatrix [0] [2] = 1;

        testDataMatrix [1] [0] = 7;
        testDataMatrix [1] [1] = 3;
        testDataMatrix [1] [2] = 1;

        testDataMatrix [2] [0] = 8;
        testDataMatrix [2] [1] = 1;
        testDataMatrix [2] [2] = 1;

        testDataMatrix [3] [0] = 4;
        testDataMatrix [3] [1] = 2;
        testDataMatrix [3] [2] = 1;

        testDataMatrix [4] [0] = 2;
        testDataMatrix [4] [1] = 4;
        testDataMatrix [4] [2] = 1;

        testDataMatrix [5] [0] = 10;
        testDataMatrix [5] [1] = 1;
        testDataMatrix [5] [2] = 1;

        testDataMatrix [6] [0] = 3;
        testDataMatrix [6] [1] = 7;
        testDataMatrix [6] [2] = 1;


        //Set up the variable types
        testVariableOrientations[0] = VariableOrientation.INPUT;
        testVariableOrientations[1] = VariableOrientation.INPUT;
        testVariableOrientations[2] = VariableOrientation.OUTPUT;
	    
        rdeaP.setDMUNames(testDMUNames);
        rdeaP.setVariableNames(testVariableNames);
        rdeaP.setDataMatrix(testDataMatrix);
        rdeaP.setVariableOrientations(testVariableOrientations);
        
	    return rdeaP;
		
	}
	
	

}
